/**
 * 
 */
package stationGUI;

import java.awt.Color;
import java.io.ObjectOutputStream;
import java.util.Vector;

/**
 * @author Owner
 *
 */
public class ServerNode {
  /**
   * 
   */
  private static final long serialVersionUID = 1L;

  NodeBean bean;
  Vector<ServerNode> neighbors;
  ObjectOutputStream out;
  
  
  public ServerNode(Color c) {
    bean=new NodeBean(c);
    neighbors=new Vector<ServerNode>();
  }
  
  public void setColor(Color c) throws Exception{
    bean.color=c;
    //TODO FIX THESE PROGRESS BAR!!!!!!
    if(bean.color.equals(bean.neighbors.get(0).color)) bean.progress=0;
    else bean.progress=1;
  //  System.out.println("setColor");
    updateNeighbors();
   // System.out.println("setColor2");
    
  }
  
  /**
   * Set the outputstream attached to this thread/node.
   * @param o
   */
  public void setOutStream(ObjectOutputStream o) {
    out=o;
  }
  
  /**
   * Update this node and all of its neighbors
   * @throws Exception
   */
  public void updateNeighbors() throws Exception {
    //setColor(c);
    //System.out.println("updateN");
    update();
    //System.out.println(neighbors.size());
    for(ServerNode current:neighbors) {
      //TODO FIX THESE PROGRESS BAR!!!!!!
      if(bean.color.equals(bean.neighbors.get(0).color)) current.bean.progress=0;
      else current.bean.progress=1;
      //System.out.println("update neighbor");
      //System.out.println("current color: "+current.bean.color);
      current.update();
    }
    //System.out.println("updateN2");
  }
  
  /**
   * Send update for this node across the server
   * @throws Exception
   */
  public void update() throws Exception {

   // System.out.println("update");
    //System.out.println("current's neighbor color through server: "+neighbors.get(0).bean.color);
    //System.out.println("current's neighbor color through bean: "+bean.neighbors.get(0).color);
    out.writeObject(new NodeBean2(bean));
    //System.out.println("current's neighbor color through server: "+neighbors.get(0).bean.color);
    //System.out.println("current's neighbor color through bean: "+bean.neighbors.get(0).color);
    // System.out.println("update2");
  }
  
  /**
   * Simply calls addDirectedEdge on this and the passed in node
   * to add a undirected edge.
   * @see addDirectedNeighbor
   * @param n
   */
  public void addNeighbor(ServerNode n) {
    addDirectedEdge(n);
    n.addDirectedEdge(this);
  }
  
  /**
   * Add a neighbor node to both the Server structure and 
   * to the underlying NodeBean structure;
   * @param n
   */
  public void addDirectedEdge(ServerNode n) {
    neighbors.add(n);
    
    bean.neighbors.add(n.bean);
    bean.numNeighbors++;
  }
}


/**
 * 
 
package stationGUI;

import java.awt.Color;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Vector;

/**
 * @author Owner
 *
 *
public class Node implements Serializable {
  /**
   * 
   
  private static final long serialVersionUID = 1L;
  public Color color;
  public Vector<Node> neighbors;
  public int numNeighbors;
  public boolean re = true;
  public boolean co = true;
  
  public NeighborhoodScreen neighborhoodScreen;
  public ColorChooserScreen colorChooserScreen;
  //ObjectOutputStream serverOut;
  //ObjectOutputStream clientOut;
  
  public Node(Color c) {
    color = c;
    neighbors = new Vector<Node>();
    numNeighbors = 0;
  }
  
  public void setColor(Color c) {
    color = c;
    co=true;
    //neighborhoodScreen.repaint();
    //colorChooserScreen.repaint();
    for(Node n: neighbors)
      n.repaintNeighborhood();
  }
  
  public void addNeighbor(Node n) {
    neighbors.add(n);
    numNeighbors++;
  }
  
  /*public void setServerOut(ObjectOutputStream out) {
    //serverOut=out;
  }
  
  public void setClientOut(ObjectOutputStream out) {
    //clientOut=out;
  }
  
  public void sendColor(Color c) throws Exception {
    //clientOut.writeObject(c);
  }
  
  public void sendColor() {
    co=true;
  }
  
  public void repaintNeighborhood() {
    //this.neighborhoodScreen.repaint();
    //serverOut.writeObject(this);
    re = true;
  }
}
*/